﻿// PythonTest.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <Python.h>
#include <vector>

using std::vector;


PyObject* helloext_example(PyObject* self, PyObject* args, PyObject* kwargs);


int main()
{
	printf("%d\n\n\n\n", sizeof(INT64*));
	printf("%d\n\n\n\n", sizeof(double*));



	Py_Initialize();//初始化python

	PyRun_SimpleString("import sys");
	PyRun_SimpleString("import os");
	PyRun_SimpleString("sys.path.append(\"./\")");

	PyRun_SimpleString("curDir = os.getcwd()");

	PyObject* ModuleMain = PyImport_GetModule(PyUnicode_DecodeLocale("__main__", NULL));



	PyObject* curDir = PyObject_GetAttrString(ModuleMain, "curDir");

	PyObject* bys = PyUnicode_EncodeLocale(curDir, NULL);

	char* szStr = PyBytes_AsString(bys);
	printf("%s\n", szStr);



	PyObject* Module_os = PyImport_GetModule(PyUnicode_DecodeLocale("os", NULL));

	PyObject* Module_dom = PyImport_Import(PyUnicode_DecodeLocale("dom", NULL));
	PyObject* Funcadd = PyObject_GetAttrString(Module_dom, "add");
	PyObject* FuncMyCall = PyObject_GetAttrString(Module_dom, "MyCall");




	if (!PyCallable_Check(Funcadd))
	{
		printf("不能找到函数!");
		return -1;
	}



	PyObject* tuple = PyTuple_New(2);
	PyObject* pyParams1 = Py_BuildValue("i", 100);
	PyObject* pyParams2 = Py_BuildValue("i", 200);

	PyTuple_SetItem(tuple, 0, pyParams1);
	PyTuple_SetItem(tuple, 1, pyParams2);


	PyObject* pRet = PyObject_CallObject(Funcadd, tuple);


	bys = PyUnicode_EncodeLocale(PyObject_Repr(pRet), NULL);
	szStr = PyBytes_AsString(bys);
	printf("%s\n", szStr);



	PyMethodDef* methoddef = new PyMethodDef();


	vector<PyMethodDef>* def = new vector<PyMethodDef>[2];
	PyMethodDef temp = {
		"efun",
		(PyCFunction)helloext_example,
		METH_VARARGS | METH_KEYWORDS,
		NULL
	};
	def->push_back(temp);
	temp.ml_doc = NULL;
	temp.ml_flags = 0;
	temp.ml_meth = NULL;
	temp.ml_name = NULL;
	def->push_back(temp);


	PyModule_AddFunctions(Module_dom, &(*def)[0]);


	pRet = PyObject_CallObject(FuncMyCall, Py_BuildValue("ii", 1999, 2999));
	bys = PyUnicode_EncodeLocale(PyObject_Repr(pRet), NULL);
	szStr = PyBytes_AsString(bys);
	printf("%s\n", szStr);




	Py_Finalize();
}


PyObject* helloext_example(PyObject* self, PyObject* args, PyObject* kwargs) {
	/* Shared references that do not need Py_DECREF before returning. */

	int arg1 = 0;
	int arg2 = 0;

	/* Parse positional and keyword arguments */
	static char* keywords[] = { "A", NULL };
	//if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", keywords, &arg1))
	{
		//	printf("PyArg_ParseTupleAndKeywords失败\n");
		//	return NULL;
	}


	//int ret =  PyArg_ParseTuple(args, "ii", &arg1, &arg2);
	
	//printf(" arg1 = %d , arg2 = %d\n", arg1,arg2);


	PyObject* obj1;
	PyObject* obj2;
	int ret =  PyArg_ParseTuple(args, "OO", &obj1, &obj2);
	printf(" arg1 = %p , arg2 = %p\n", obj1,obj2);


	
	printf(" arg1 = %d , arg2 = %d\n", PyLong_AS_LONG(obj1), PyLong_AS_LONG(obj2));
	


	///* Function implementation starts here */

// 	//if (number < 0) {
// 	//	PyErr_SetObject(PyExc_ValueError, obj);
// 	//	return NULL;    /* return NULL indicates error */
// 	//}



	return PyLong_FromLong(100);

	return Py_BuildValue("s", PyUnicode_DecodeLocale("中文啦啦啦abcAZ09!@$%^&*()", NULL));


	Py_RETURN_NONE;
}